<template>
    <el-container>
        <el-header class="header-tabs">
            <el-tabs type="card" v-model="groupId" @tab-change="tabChange">
                <el-tab-pane label="全部回款" name="-1">
                    <template #label>
                        <el-badge :value="headnumber.total" class="item" :max="9999">
                            <div style="width:60px">全部回款</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="待审核" name="0">
                    <template #label>
                        <el-badge :value="headnumber.not_check" class="item" :max="9999">
                            <div style="width:50px">待审核</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="驳回" name="1">
                    <template #label>
                        <el-badge :value="headnumber.reject" class="item" :max="9999">
                            <div style="width:40px">驳回</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="审核通过" name="2">
                    <template #label>
                        <el-badge :value="headnumber.adopt" class="item" :max="9999">
                            <div style="width:60px">审核通过</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </el-header>
        <el-header>
            <div class="left-panel">
                <!-- <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'custom.add'"></el-button> -->
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'paymentrecord.delete'"></el-button>
                <!-- <sc-file-import :apiObj="$API.order.entrust.orderUp" @success="success" ref="importBox" templateUrl="https://2156.mzth.cn/import.xlsx"></sc-file-import> -->
            </div>
            <div class="right-panel">
                <div class="right-panel-search" style="flex-wrap:wrap;">
                    <el-input style="margin:5px 10px" v-model="search.project_code" placeholder="请输入项目编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.company_name" placeholder="请输入收款账户" clearable @keyup.enter="upsearch"></el-input>
                    <!-- <el-input style="margin:5px 10px" v-model="search.dept_id" placeholder="所属部门" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.salesman" placeholder="商务" clearable @keyup.enter="upsearch"></el-input> -->
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column label="项目编号" prop="project.project_code" align="center"></el-table-column>
                <el-table-column label="项目名称" prop="project.metting" align="center"></el-table-column>
                <el-table-column label="公司名称" prop="customer.company_name" align="center"></el-table-column>
                <el-table-column label="应收金额" prop="collect.collect_account" align="center"></el-table-column>
                <el-table-column label="回款金额" prop="money" align="center"></el-table-column>
                <!-- <el-table-column label="付款人名称" prop="pay_name" align="center"></el-table-column> -->
                <el-table-column label="付款时间" prop="pay_time" align="center"></el-table-column>
                <el-table-column label="收款账户" prop="company.company_name" align="center"></el-table-column>
                <el-table-column label="收款方式" prop="pay_type" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.pay_type == 1">公户</div>
                        <div v-if="scope.row.pay_type == 2">微信(支付宝)</div>
                        <div v-if="scope.row.pay_type == 3">银行卡</div>
                        <div v-if="scope.row.pay_type == 4">二维码</div>
                    </template>
                </el-table-column>
                <el-table-column label="回款凭证" prop="pay_files" align="center">
                    <template #default="scope">
                        <el-image class="img" v-if="scope.row.pay_files" :src="scope.row.pay_files" :preview-src-list="scope.row.pay_files" fit="cover" preview-teleported="true" hide-on-click-modal />
                    </template>
                </el-table-column>
                <!-- <el-table-column label="回款说明" prop="remark" align="center"></el-table-column> -->
                <el-table-column label="审核状态" prop="check_status" align="center">
                    <template #default="scope">
                        <el-tag :type="scope.row.check_status == 0 ?'warning' : scope.row.check_status == 2 ? 'success' : 'danger'">
                            {{scope.row.check_status == 0 ?'待审核' : scope.row.check_status == 2 ? '审核通过' : '审核拒绝'}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="170">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'paymentrecord.edit'" v-if="scope.row.check_status == 0">编辑</el-button>
                            <el-button text type="warning" size="small" @click="table_check(scope.row, scope.$index)" v-auth="'paymentrecord.check'" v-if="scope.row.check_status == 0">审核</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'paymentrecord.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <check-status v-if="dialog.status" ref="statusDialog" @success="handleSuccess" @closed="dialog.status = false" draggable></check-status>
</template>

<script>
import CheckStatus from '../checkStatus.vue'
import saveDialog from './save.vue'

export default {
	name: '客户管理',
	components: {
		saveDialog,
CheckStatus,
	},
	data() {
		return {
			dialog: {
				save: false,
				status: false,
			},
			apiObj: this.$API.project.payment.list,
			selection: [],
			search: {
				check_status: null,
				project_code: null,
				company_name: null,
			},
            groupId:"-1",
            headnumber:{},
		}
	},
	mounted() {
        this.getList()
	},
	methods: {
        async getList(){
            var res = await this.$API.project.payment.list.get();
            this.headnumber = res.data.extend
        },
        success(res){
			if(res.code==1){
				this.$refs.table.refresh()
                this.getList()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
        table_check(row) {
			this.dialog.status = true
			this.$nextTick(() => {
				this.$refs.statusDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
            this.client_id = row.id
			this.dialog.details = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.project.payment.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
                this.getList()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.project.payment.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
                this.getList()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
            this.getList()
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        tabChange(e){
            this.groupId = e
            this.search.check_status = e
            this.upsearch()
        },
	}
}
</script>

<style>
.titleBox {
	font-size: 14px;
	font-weight: 500;
	color: #0099ff;
	margin-left: 30px;
}
</style>
